package demo.xlj.yuyi.studentapp.dudaoyuan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.NoticeBean;
import demo.xlj.yuyi.studentapp.greendao.AnnouncenmentNewDao;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import top.wefor.circularanim.CircularAnim;

/**
 * 发布公告
 */
public class FaBuAnnouncementActivity extends BaseRxActivity {

    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.EditText_Fa_Bu_Announcement_Title)
    EditText mEditTextFaBuAnnouncementTitle;
    @BindView(R.id.EditText_Fa_Bu_Announcement_Person)
    EditText mEditTextFaBuAnnouncementPerson;
    @BindView(R.id.EditText_Fa_Bu_Announcement_Content)
    EditText mEditTextFaBuAnnouncementContent;
    @BindView(R.id.progressBar_student_meet_login)
    ProgressBar mProgressBarStudentMeetLogin;
    @BindView(R.id.button_Fa_Bu_Announcement)
    Button mButtonFaBuAnnouncement;


    private AnnouncenmentNewDao mAnnouncenmentNewDao;


    private boolean isFaBu = false;

    private String title;
    private String person;
    private String content;
    private String time;

    private String type;

    private String uid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fa_bu_announcement);
        ButterKnife.bind(this);
//        mAnnouncenmentNewDao = BaseApplication.instances.getDaoSession().getAnnouncenmentNewDao();

        uid = (String) SharePerferencesUtils.get(FaBuAnnouncementActivity.this, "name", "99", 0);

        Intent intent = getIntent();

        type = intent.getStringExtra("type");

        if (type.equals("1")){
            mTextViewTitle.setText("发布社团通知");
        }else if (type.equals("2")){
            mTextViewTitle.setText("发布学生会通知");
        }else {

        }


    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        if (!isFaBu) {
            FaBuAnnouncementActivity.this.finish();
        } else {
            ToastUtil.info(FaBuAnnouncementActivity.this, "您正在发布，不能退出，请耐心等待！", true, true);
        }

    }



    /**
     * 发布提交的按钮
     *
     * @param view
     */
    public void btn_Fa_Bu_Announcement(View view) {
        title = mEditTextFaBuAnnouncementTitle.getText().toString();
        person = mEditTextFaBuAnnouncementPerson.getText().toString();
        content = mEditTextFaBuAnnouncementContent.getText().toString();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        time = formatter.format(curDate);

        if (TextUtils.isEmpty(title)) {
            mEditTextFaBuAnnouncementTitle.setError("主题不能为空");
            return;
        }

        if (TextUtils.isEmpty(person)) {
            mEditTextFaBuAnnouncementPerson.setError("发布人不能为空");
            return;
        }

        if (TextUtils.isEmpty(content)) {
            mEditTextFaBuAnnouncementContent.setError("公告内容不能为空");
            return;
        }

        //开启动画
        CircularAnim.hide(mButtonFaBuAnnouncement).go();

        isFaBu = true;

        mHandler.sendEmptyMessageDelayed(0, 618);

    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mProgressBarStudentMeetLogin.setVisibility(View.VISIBLE);


                    addRxDestroy(RetrofitClient.getInstance(FaBuAnnouncementActivity.this)
                            .getApiService()
                            .addNotice(title, person, content,time,uid,type)
                            .compose(RxSchedulers.<NoticeBean>io_main())
                            .subscribe(new Consumer<NoticeBean>() {
                                @Override
                                public void accept(@NonNull NoticeBean noticeBean) throws Exception {
                                    if (noticeBean.getStatus() == 10014) {
                                        ToastUtil.success(FaBuAnnouncementActivity.this, "发布成功", false, true);
                                        isFaBu = false;
                                    } else {
                                        ToastUtil.success(FaBuAnnouncementActivity.this, "发布失败", false, true);
                                        isFaBu = false;
                                    }
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(@NonNull Throwable throwable) throws Exception {
                                    ToastUtil.success(FaBuAnnouncementActivity.this, "发布失败", false, true);
                                    isFaBu = false;
                                }
                            }));
                           mEditTextFaBuAnnouncementTitle.setText("");
                           mEditTextFaBuAnnouncementContent.setText("");
                           mEditTextFaBuAnnouncementPerson.setText("");
                           mProgressBarStudentMeetLogin.setVisibility(View.INVISIBLE);
                           CircularAnim.show(mButtonFaBuAnnouncement).go();

                    //操作数据库

//                    AnnouncenmentNew mAnnouncement = new AnnouncenmentNew(null,title,person,content,time);
//
//                    mAnnouncenmentNewDao.insert(mAnnouncement);


                    break;


            }

        }
    };
}
